<template>
  <div class="app-container">
    <data-table
      v-loading="listLoading"
      ref="dataTable"
      :key="tableKey"
      :url="url"
      :border="false"
      :stripe="true"
      row-key="id"
      size="small"
      style="width: 100%;">

      <el-table-column label="编号" prop="id" align="center" width="100px"/>
      <el-table-column label="项目名称" prop="name" align="center" min-width="100px"/>
      <el-table-column label="缩写" prop="prefix" align="center" width="60px"/>
      <el-table-column label="开始时间" prop="startDate" align="center" min-width="100px"/>
      <el-table-column label="描述" prop="description" align="center" min-width="200px"/>
      <el-table-column v-if="canEdit" label="操作" align="center" width="100px" class-name="small-padding fixed-width" >
        <template slot="header" slot-scope="scope">
          <el-button v-if="canAdd" type="text" size="mini" @click="handleCreate">添加</el-button>
          <template v-else>{{ scope.column.label }}</template>
        </template>
        <template slot-scope="scope" >
          <el-button v-if="canUpdate" type="text" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <confirm-poptip v-if="canDelete" type="text" size="mini" @on-ok="handleDelete(scope.row)"/>
        </template>
      </el-table-column>
    </data-table>

    <el-dialog v-el-drag-dialog :title="dialogFormTitle" :visible.sync="dialogFormVisible" :close-on-click-modal="false" width="400px">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="right" label-width="80px" >

        <el-form-item v-if="!isNew" label="编号" prop="id">
          <el-input v-model="temp.id" :max-length="30" :disabled="true"/>
        </el-form-item>
        <el-form-item label="项目名称" prop="name">
          <el-input v-model="temp.name" :max-length="20"/>
        </el-form-item>
        <el-form-item label="缩写" prop="prefix">
          <el-input v-model="temp.prefix" :max-length="20"/>
        </el-form-item>
        <el-form-item label="开始时间" prop="startDate">
          <el-date-picker v-model="temp.startDate" type="date"/>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="temp.description" :max-length="20"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="isNew?createData():updateData()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { add, update, remove } from '@/api/projects'
import waves from '@/directive/waves' // 水波纹指令
import elDragDialog from '@/directive/el-dragDialog'
import { canAdd, canUpdate, canDelete, parseTime } from '@/utils'

export default {
  name: 'Projects',
  directives: {
    waves, elDragDialog
  },
  components: { },
  filters: {
  },
  data() {
    return {
      controllerName: 'Projects',
      url: 'projects',
      showDetail: false,
      detailId: null,
      tableKey: 0,
      listLoading: false,
      queryInfo: null,

      defaultForm: {
        id: undefined,
        name: null,
        prefix: null,
        startDate: null,
        description: null
      },
      temp: {
      },

      rules: {
        name: [{ required: true, message: '不能为空' }],
        prefix: [{ required: true, message: '不能为空' }],
        startDate: [{ required: true, message: '不能为空' }]
      },
      dialogFormVisible: false,
      isNew: false
    }
  },

  computed: {
    canEdit() {
      return this.canAdd || this.canUpdate || this.canDelete
    },
    canAdd() {
      return canAdd(this.$store.state, this.controllerName)
    },
    canUpdate() {
      return canUpdate(this.$store.state, this.controllerName)
    },
    canDelete() {
      return canDelete(this.$store.state, this.controllerName)
    },
    dialogFormTitle() {
      return this.isNew ? '新建' : '编辑'
    }
  },
  watch: {
  },
  created() {
  },
  methods: {

    handleQuery() {
      this.$refs.dataTable.setQuery(this.queryInfo)
    },
    resetTemp() {
      this.temp = JSON.parse(JSON.stringify(this.defaultForm))
    },
    handleCreate() {
      this.resetTemp()
      this.isNew = true
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.temp.startDate) {
            this.temp.startDate = parseTime(this.temp.startDate, '{y}-{m}-{d}')
          }
          add(this.temp).then(res => {
            this.$notify({
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
            this.dialogFormVisible = false
            this.$refs.dataTable.getList()
          })
        }
      })
    },
    handleUpdate(row) {
      this.isNew = false
      this.dialogFormVisible = true
      this.temp = Object.assign({}, row)
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.temp.startDate) {
            this.temp.startDate = parseTime(this.temp.startDate, '{y}-{m}-{d}')
          }
          update(this.temp).then(res => {
            this.$notify({
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
            this.dialogFormVisible = false
            this.$refs.dataTable.getList()
          })
        }
      })
    },
    handleDelete(row) {
      remove(row.ID).then(() => {
        this.$notify({
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        this.$refs.dataTable.deleteData(row)
      })
    }
  }
}

</script>
